#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import math


def node_proc(x):
    y = 1.0/(1.0+np.exp(-x))
    return y


def judge(term, x, y, o, wxy, wyo, XNUM, YNUM):
    # input term of data
    xit = [term.nseismic, term.nseismo, term.nshift,
           math.log10(term.genergy+1.0), math.log10(term.gpuls+1.0),
           term.gdenergy, term.gdpuls,
           term.nghazard] + term.nbumps +\
           [math.log10(term.energy+1.0), math.log10(term.maxenergy+1.0)]
    # layer by layer
    x = np.matrix(xit)
    yi = np.dot(x, wxy)
    y = node_proc(yi)
    oi = np.dot(y, wyo)
    o = node_proc(oi)
    return o[0, 0]


def count(o, d, mat):
    lim = 0.5
    if o >= lim and d >= lim:
        mat['TP'] += 1
    elif o < lim and d >= lim:
        mat['FN'] += 1
    elif o >= lim and d < lim:
        mat['FP'] += 1
    else:
        mat['TN'] += 1


def stat(testmat, d):
    testTrue = testmat['TP'] + testmat['TN']
    testTotal = testmat['TP'] + testmat['TN'] + testmat['FP'] + testmat['FN']
    v = float(testmat['TP']+testmat['FP'])
    if v == 0:
        d['P'] = -1.0
    else:
        d['P'] = float(testmat['TP'])/v
    v = float(testmat['TP']+testmat['FN'])
    if v == 0:
        d['R'] = -1.0
    else:
        d['R'] = float(testmat['TP'])/v
    d['acc'] = testTrue/float(testTotal)
    d['E'] = 1.0-d['acc']
    d['beta'] = 2.0
    v = float(d['beta']**2.0*d['P'] + d['R'])
    if v == 0:
        d['F_beta'] = -1.0
    else:
        d['F_beta'] = ((1.0+d['beta']**2.0)*d['P']*d['R']) / v
